package com.glory.wxelectricitypda.activity;


import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.InboundDetailsAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.InboundDetailsBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FinishedProductStorageActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.packLotId)
    FreeUI_ClearEditText packLotId;
    @BindView(R.id.flStepName)
    FrameLayout flStepName;
    @BindView(R.id.tvwarehouse)
    TextView tvwarehouse;
    @BindView(R.id.ivwarehouse)
    ImageView ivwarehouse;
    @BindView(R.id.inboundDetailsRecycler)
    RecyclerView inboundDetailsRecycler;
    @BindView(R.id.emptyList)
    TextView emptyList;

    private List<String> warehouseList = new ArrayList<>();
    private FreeUI_GeneralPop mWarehouseFalgPop;
    private InboundDetailsAdapter mInboundDetailsAdapter;
    private List<InboundDetailsBean> mInboundDetailsBeanList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_finished_product_storage;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("成品入库");

        inboundDetailsRecycler.setLayoutManager(new LinearLayoutManager(FinishedProductStorageActivity.this));
        inboundDetailsRecycler.setItemAnimator(new DefaultItemAnimator());
        inboundDetailsRecycler.addItemDecoration(new DividerItemDecoration(FinishedProductStorageActivity.this,1));
        mInboundDetailsAdapter = new InboundDetailsAdapter(null);
        mInboundDetailsAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mInboundDetailsAdapter.isFirstOnly(true);
        inboundDetailsRecycler.setAdapter(mInboundDetailsAdapter);


    }

    @Override
    protected void initData() {

        initEdit(packLotId,s->{


            queryInboundDetails();

        });

        flStepName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mWarehouseFalgPop != null){
                    if (mWarehouseFalgPop.isShowing()){
                        mWarehouseFalgPop.dismiss();
                    }
                }else {
                    mWarehouseFalgPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flStepName.getWidth(), warehouseList, ivwarehouse, new FreeUI_TextCallback() {
                        @Override
                        public void onSuccess(String s) {

                            tvwarehouse.setText(s);
                        }
                    });
                }

                FreeApi_PopUtils.setArrowShow(ivwarehouse);
                mWarehouseFalgPop.showAsDropDown(flStepName,0,0);
            }
        });
    }

    public void queryInboundDetails(){

        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("ACTIONTYPE","SHIP");
            jsonObject1.put("ACTIONCOMMENT","");
            jsonObject1.put("PACKLOTID",packLotId.getText().toString());
            jsonObject1.put("WAREHOUSE_CODE",tvwarehouse.getText().toString());
            jsonObject.put("Data",jsonObject1);
            jsonObject.put("MessageName","EUIT.EUITFCSHIP");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<InboundDetailsBean>>(
                new TypeToken<ResponseBean<InboundDetailsBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<InboundDetailsBean>> response) {
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    mInboundDetailsBeanList.add(0,response.body().getBody());
                    mInboundDetailsAdapter.setNewData(mInboundDetailsBeanList);
                    packLotId.setText("");
                }
            }

            @Override
            public void onError(Response<ResponseBean<InboundDetailsBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.EUITFCSHIP");
            }
        });



    }

    @OnClick({R.id.emptyList})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.emptyList:
                mInboundDetailsBeanList.clear();
                mInboundDetailsAdapter.setNewData(mInboundDetailsBeanList);
                break;
        }
    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header",llMain);
    }
}